<?php

/**
 * comment actions.
 *
 * @package    wills
 * @subpackage comment
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CommentActions extends sfActions
{
  public function preExecute()
  {
    $this->article = Doctrine::getTable('Article')->findOneBy(
      'id', 
      $this->getRequest()->getParameter('article_id')
    );

    $this->forward404Unless($this->article);
  }

  public function executeIndex(sfWebRequest $request)
  {
	 $this->comments = $this->article
      ->getCommentsWithAuthorQuery()->execute();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
	$this->form = new CommentForm();
    $this->processForm($request, $this->form);
    $this->setTemplate('new');

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
	//$this->form->bind(array('captcha'   => $request->getParameter('captcha'),));

    $form->bind($request->getParameter($form->getName()),$request->getFiles($form->getName()));
    $comment = $form->getObject()
        ->setArticleId($request->getParameter('article_id'));
	if ($form->isValid())
    {
      $form->save();
	  $this->getUser()->setFlash('message', '评论成功！');
      $this->redirect('@article_showcomments?articleid=' . $comment->getArticleId());
    }

  }
}
